<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <a-spin :spinning="isLoading">
      <a-form :form="form" @submit="handleSubmit">
        <!-- <a-form-item label="最早时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-time-picker
            format="HH:mm"
            v-decorator="['startTime', { rules: [{ required: true, message: '不能为空' }] }]"
          />
        </a-form-item>
        <a-form-item label="最晚时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-time-picker
            format="HH:mm"
            v-decorator="['endTime', { rules: [{ required: true, message: '不能为空' }] }]"
          />
        </a-form-item> -->
        <a-form-item label="预约说明" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['explain', { rules: [{ required: true, message: '不能为空' }] }]" placeholder="请输入预约说明" :auto-size="{ minRows: 3, maxRows: 5 }" />
        </a-form-item>

        <a-form-item :wrapper-col="{ span: wrapperCol.span, offset: labelCol.span }">
          <a-button type="primary" html-type="submit">提交</a-button>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-card>
</template>

<script>
import moment from 'moment'
import pick from 'lodash.pick'
import * as Api from '@/api/appointment/setting'
import { isEmpty } from '@/utils/util'

export default {
  data() {
    return {
      // 当前设置项的key
      key: 'trade',
      // 标签布局属性
      labelCol: { span: 4 },
      // 输入框布局属性
      wrapperCol: { span: 10 },
      // loading状态
      isLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 当前记录详情
      record: {},
    }
  },
  // 初始化数据
  created() {
    // 获取当前详情记录
    this.getDetail()
  },
  methods: {
    // 获取当前详情记录
    getDetail() {
      this.isLoading = true
      Api.info()
        .then((result) => {
          // 当前记录
          this.record = result.data.setting
          // 设置默认值
          this.setFieldsValue()
        })
        .finally((result) => {
          this.isLoading = false
        })
    },

    /**
     * 设置默认值
     */
    setFieldsValue() {
      const { record, $nextTick, form } = this
      !isEmpty(form.getFieldsValue()) &&
        $nextTick(() => {
          form.setFieldsValue({
            // startTime: moment(record.startTime, 'HH:mm'),
            // endTime: moment(record.endTime, 'HH:mm'),
            explain: record.explain
          })
        })
    },

    /**
     * 确认按钮
     */
    handleSubmit(e) {
      e.preventDefault()
      // 表单验证
      const {
        form: { validateFields },
      } = this
      validateFields((errors, values) => {
        // values.startTime = values.startTime.format('HH:mm')
        // values.endTime = values.endTime.format('HH:mm')
        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },

    /**
     * 提交到后端api
     */
    onFormSubmit(values) {
      this.isLoading = true
      Api.update({ form: values })
        .then((result) => {
          // 显示提示信息
          this.$message.success(result.message, 1.5)
        })
        .finally((result) => {
          this.isLoading = false
        })
    },
  },
}
</script>
<style lang="less" scoped>
/deep/.ant-form-item-control {
  padding-left: 10px;

  .ant-form-item-control {
    padding-left: 0;
  }
}
.ant-divider {
  margin-top: 60px !important;
}
.ant-input-number {
  width: 160px;
}
.input-text_right {
  margin-left: 10px;
}
.radio-item {
  margin-bottom: 16px;
}
</style>
